import { request } from "../../request/index.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
      leftMenuList:[],
      rightContent:[],
      topIndex:0
  },
  Cates:[],

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const cates = wx.getStorageSync('Cates');
    if(cates){
      this.Cates = cates;
      //构造左侧大数据
      let leftMenuList = this.Cates.map(function(v){
        return v.cat_name;
      })
      let rightContent = this.Cates[0].children;
      this.setData({
          leftMenuList,
          rightContent
        })
    }else{
      this.getCateList();
    }
    
  },
  // 获取商品分类
  getCateList:function(){
    const that = this;
        request({
          url:"https://api-hmugo-web.itheima.net/api/public/v1/categories"
        }).then(function(res){
            that.Cates = res.data.message;
            wx.setStorageSync('Cates',res.data.message);
            //构造左侧大数据
              let leftMenuList = that.Cates.map(function(v){
                return v.cat_name;
            })
              let rightContent = that.Cates[0].children;
                that.setData({
                  leftMenuList,
                  rightContent
                })
        })
  },
  getGoodsList:function(e){
    const id = e.currentTarget.dataset.id;
    let rightContent = this.Cates[id].children;
    this.setData({
      rightContent,
      topIndex:0
    })
  }
})